/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.DisciplinaDao;
import Dao.DisciplinaDaoImplJPA;
import Dao.TurmaDao;
import Dao.TurmaDaoImplJPA;
import Pojo.Disciplina;
import Pojo.Turma;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author thiago
 */
public class FormCadastroDisciplinas extends javax.swing.JInternalFrame {
/** String de modificação N = novo, A = alteração*/
    String modificacao;

    /**
     * Setter de modificação
     * @param modificacao 
     */
    public void setModificacao(String modificacao) {
        this.modificacao = modificacao;
    }

    /**
     * Getter de modificação
     * @return 
     */
    public String getModificacao() {
        return modificacao;
    }
    
    /**
     * Creates new form FormCadastroDisciplinas
     */
    public FormCadastroDisciplinas() {
        initComponents();
    }
    
    /**
     * Creates new form FormCadastroDisciplinas
     */
    public FormCadastroDisciplinas(int codigo) {
        initComponents();
        txtCodigo.setText(String.valueOf(codigo));
        this.carregarDadosCampos(codigo);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtEmenta = new javax.swing.JTextField();
        lblEmenta = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        lblNome = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        lblCodigo = new javax.swing.JLabel();
        txtCargaHoraria = new javax.swing.JTextField();
        lblCargaHoraria = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnNovo = new javax.swing.JButton();
        btnAlterar = new javax.swing.JButton();
        btnRemover = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Cadastro de Disciplinas");
        setPreferredSize(new java.awt.Dimension(376, 288));

        txtEmenta.setEnabled(false);

        lblEmenta.setText("Ementa");

        txtNome.setEnabled(false);

        lblNome.setText("Nome");

        txtCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCodigoKeyPressed(evt);
            }
        });

        lblCodigo.setText("Código");

        txtCargaHoraria.setEnabled(false);

        lblCargaHoraria.setText("Carga Horária");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNome)
                    .addComponent(txtEmenta)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtCargaHoraria, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNome, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblEmenta, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCodigo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtCodigo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblCargaHoraria, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(0, 86, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(lblEmenta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtEmenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblCargaHoraria)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCargaHoraria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnNovo.setText("Novo");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnAlterar.setText("Alterar");
        btnAlterar.setEnabled(false);
        btnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAlterarActionPerformed(evt);
            }
        });

        btnRemover.setText("Remover");
        btnRemover.setEnabled(false);
        btnRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoverActionPerformed(evt);
            }
        });

        btnSalvar.setText("Salvar");
        btnSalvar.setEnabled(false);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setText("Cancelar");
        btnCancelar.setEnabled(false);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNovo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemover, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalvar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAlterar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnRemover)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSalvar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancelar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void carregarDadosCampos(int codigo){
        DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
        Disciplina disciplina = disciplinaDao.pesquisarPorId(codigo);

        if (disciplina != null){
            txtNome.setText(disciplina.getNome());
            txtEmenta.setText(disciplina.getEmenta());
            txtCargaHoraria.setText(String.valueOf(disciplina.getCargaHoraria()));

            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(true);
            btnRemover.setEnabled(true);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
        } else {
            txtNome.setText("");
            txtEmenta.setText("");
            txtCargaHoraria.setText("");

            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
            JOptionPane.showMessageDialog(null, "Não existe uma disciplina com esse código.");
        }
    }
    
    private void txtCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoKeyPressed
        if (evt.getKeyCode()==10){
            int codigo;
            try{
                codigo = Integer.parseInt(txtCodigo.getText());
            }catch(Exception e){
                return;
            }
            this.carregarDadosCampos(codigo);
        }
    }//GEN-LAST:event_txtCodigoKeyPressed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed

        txtCodigo.setEnabled(false);
        txtNome.setEnabled(true);
        txtEmenta.setEnabled(true);
        txtCargaHoraria.setEnabled(true);

        txtNome.setText("");
        txtEmenta.setText("");
        txtCargaHoraria.setText("");

        btnNovo.setEnabled(false);
        btnAlterar.setEnabled(false);
        btnRemover.setEnabled(false);
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);

        txtNome.grabFocus();
        this.setModificacao("N");
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAlterarActionPerformed
        if (txtCodigo.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe um disciplina para ser alterado.");
        }else{
            txtCodigo.setEnabled(false);
            txtNome.setEnabled(true);
            txtEmenta.setEnabled(true);
            txtCargaHoraria.setEnabled(true);

            btnNovo.setEnabled(false);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(true);
            btnCancelar.setEnabled(true);

            txtNome.grabFocus();
            this.setModificacao("A");
        }
    }//GEN-LAST:event_btnAlterarActionPerformed

    private void btnRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoverActionPerformed
        if (txtCodigo.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe uma disciplina para ser removido.");
        }else{           
            if (JOptionPane.showConfirmDialog(null, "Deseja remover o disciplina?")==0){                
                Disciplina disciplina = new Disciplina(Integer.parseInt(txtCodigo.getText()),txtNome.getText(),txtEmenta.getText(),Integer.parseInt(txtCargaHoraria.getText()));
                DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
                
                TurmaDao turmaDao = new TurmaDaoImplJPA();
                List<Turma> listaTurmasDisciplina = turmaDao.pesquisarPorDisciplina(disciplina);
                if (listaTurmasDisciplina.size()>0){
                    JOptionPane.showMessageDialog(null, "Impossível remover esta disciplina, pois esta tem turmas cadastradas.");
                    return;
                }
                
                if (disciplinaDao.remover(disciplina)==true){

                    txtCodigo.setEnabled(true);
                    txtNome.setEnabled(false);
                    txtEmenta.setEnabled(false);
                    txtCargaHoraria.setEnabled(false);

                    txtCodigo.setText("");
                    txtNome.setText("");
                    txtEmenta.setText("");
                    txtCargaHoraria.setText("");

                    btnNovo.setEnabled(true);
                    btnAlterar.setEnabled(false);
                    btnRemover.setEnabled(false);
                    btnSalvar.setEnabled(false);
                    btnCancelar.setEnabled(false);

                    JOptionPane.showMessageDialog(null, "Disciplina removido com sucesso.");
                }else{
                    JOptionPane.showMessageDialog(null, "Erro ao remover o disciplina.");
                }
            }
        }
    }//GEN-LAST:event_btnRemoverActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed

        DisciplinaDao disciplinaDao = new DisciplinaDaoImplJPA();
        Disciplina disciplina;
        boolean salvo = false;

        if (txtNome.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe o nome da disciplina.");
            return;
        }

        if (txtEmenta.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe a ementa da disciplina.");
            return;
        }
        
        if (txtCargaHoraria.getText().length()==0){
            JOptionPane.showMessageDialog(null, "Informe a carga horária da disciplina.");
            return;
        }

        if (this.getModificacao().equals("A")){
            disciplina = new Disciplina(Integer.parseInt(txtCodigo.getText()),txtNome.getText(),txtEmenta.getText(),Integer.parseInt(txtCargaHoraria.getText()));
            if (disciplinaDao.alterar(disciplina)==true){
                JOptionPane.showMessageDialog(null, "Disciplina alterada com sucesso.");
                salvo = true;
            }else{
                JOptionPane.showMessageDialog(null, "Erro ao alterar a disciplina.");
                salvo = false;
            }
        }else if (this.getModificacao().equals("N")){
            disciplina = new Disciplina(txtNome.getText(),txtEmenta.getText(),Integer.parseInt(txtCargaHoraria.getText()));

            if (disciplinaDao.inserir(disciplina)==true){
                JOptionPane.showMessageDialog(null, "Disciplina inserida com sucesso.");
                salvo = true;
            }else{
                JOptionPane.showMessageDialog(null, "Erro ao inserir a disciplina.");
                salvo = false;
            }
        }

        if (salvo == true){
            txtCodigo.setEnabled(true);
            txtNome.setEnabled(false);
            txtEmenta.setEnabled(false);
            txtCargaHoraria.setEnabled(false);

            txtCodigo.setText("");
            txtNome.setText("");
            txtEmenta.setText("");
            txtCargaHoraria.setText("");
            
            btnNovo.setEnabled(true);
            btnAlterar.setEnabled(false);
            btnRemover.setEnabled(false);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);

            this.setModificacao("");

            txtCodigo.grabFocus();
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        txtCodigo.setEnabled(true);
        txtNome.setEnabled(false);
        txtEmenta.setEnabled(false);
        txtCargaHoraria.setEnabled(false);

        txtCodigo.setText("");
        txtNome.setText("");
        txtEmenta.setText("");
        txtCargaHoraria.setText("");

        btnNovo.setEnabled(true);
        btnAlterar.setEnabled(false);
        btnRemover.setEnabled(false);
        btnSalvar.setEnabled(false);
        btnCancelar.setEnabled(false);
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAlterar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblCargaHoraria;
    private javax.swing.JLabel lblCodigo;
    private javax.swing.JLabel lblEmenta;
    private javax.swing.JLabel lblNome;
    private javax.swing.JTextField txtCargaHoraria;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtEmenta;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
